﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using SachOnlineLibrary.Public;
/// <summary>
/// Summary description for DangKiBUS
/// </summary>
namespace SachOnlineLibrary.DAL
{
    public class TaiKhoanDAL
    {
        KetNoiDB db;
        public TaiKhoanDAL()
        {
            db = new KetNoiDB();
        }
        public bool TaoTaiKhoan(TaiKhoan tk)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = @"insert TaiKhoan values (@TenTK,@MatKhau,0, @Ten,@NgaySinh, @Email,@SoDT,@DiaChi,1)";
            // INSERT KHACHHANG VALUES (N'KH01', N'KH01',N'Nguyễn Văn A','01221111111','khach1@gmail.com',N'TP.HCM')
         
            cmd.Parameters.AddWithValue("@TenTK", tk.TenTK);
            cmd.Parameters.AddWithValue("@MatKhau", tk.MatKhau);
            cmd.Parameters.AddWithValue("@Quyen", tk.Quyen);
            cmd.Parameters.AddWithValue("@Ten", tk.Ten);
            cmd.Parameters.AddWithValue("@NgaySinh", tk.NgaySinh);
            cmd.Parameters.AddWithValue("@SoDT", tk.SoDT);
            cmd.Parameters.AddWithValue("@Email", tk.Email);
            cmd.Parameters.AddWithValue("@DiaChi", tk.DiaChi);
           
            return db.ExecuteNonQuery(cmd);
        }
        public bool SuaTaiKhoan(string tentk, string matkhau, string ten, DateTime ngaysinh, string email, string sodt, string diachi)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "update taikhoan set matkhau = @matkhau,ten =@ten,ngaysinh = @ngaysinh,email = @email, sodt = @sodt, diachi = @diachi  where tentk = @tentk";
            cmd.Parameters.Add("@tentk", SqlDbType.VarChar).Value = tentk;
            cmd.Parameters.Add("@matkhau", SqlDbType.VarChar).Value = matkhau;
            cmd.Parameters.Add("@ten", SqlDbType.NVarChar).Value = ten;
            cmd.Parameters.Add("@ngaysinh", SqlDbType.DateTime).Value = ngaysinh;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = email;
            cmd.Parameters.Add("@sodt", SqlDbType.NVarChar).Value = sodt;
            cmd.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = diachi;
            return db.ExecuteNonQuery(cmd);
        }
        public bool KichHoatTaiKhoan(int ma, bool trangthai)
        {          
            SqlCommand cmd = new SqlCommand();           
            cmd.CommandText = "update taikhoan set TrangThai = @TrangThai where TKID = @TKID";
            cmd.Parameters.Add("@TKID", SqlDbType.Int).Value = ma;   
             cmd.Parameters.Add("@TrangThai", SqlDbType.Bit).Value = trangthai;  
            return db.ExecuteNonQuery(cmd);
        }
        public DataTable LayDSTaiKhoan()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from taikhoan where Quyen = 0";           
            return db.ExecuteQuery(cmd);
        }
        public DataTable LayTaiKhoan(string tentk, string matkhau)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from taikhoan where tentk=@tentk and matkhau=@matkhau and TrangThai = 1";
            cmd.Parameters.Add("@tentk",SqlDbType.VarChar).Value=tentk;
            cmd.Parameters.Add("@matkhau", SqlDbType.VarChar).Value = matkhau;
            return db.ExecuteQuery(cmd);
        }
        public DataTable LayTaiKhoanTheoTen(string tentk)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from taikhoan where tentk=@tentk";
            cmd.Parameters.Add("@tentk", SqlDbType.VarChar).Value = tentk;
            return db.ExecuteQuery(cmd);
        }
        public DataTable LayTaiKhoanTheoEmail(string email)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from taikhoan where Email= @email";
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = email;
            return db.ExecuteQuery(cmd);
        }
        public DataTable LayTaiKhoanTheoID(int tkid)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from taikhoan where tkid=@tkid";
            cmd.Parameters.Add("@tkid", SqlDbType.Int).Value = tkid;
            return db.ExecuteQuery(cmd);
        }
        
    }
}